<?php
/*
 * @param author feyeen date 20160425
 * @param address wap
 */
namespace wap\controllers;

use Yii;
use services\models\OrderInfoDetail;
use services\models\OrderInfo;
use services\models\Order;
use yii\web\NotFoundHttpException;
use common\models\OrderInfoHistory;
class OrderDetailsController extends CustomController{


    //订单列表
    public function actionListwap(){
        //验证信息
        if(self::verification()==false)
            return  $this->redirect($this->login_url);

        return $this->render('listwap');
    }

    //订单列表Ajax
    public function actionListwapAjax(){

        //验证信息
        if(self::verification()==false)
            return null;

        //查询订单
        $params['bestry_uid'] = $this->bestry_uid;
        //分页问题
        $page = Yii::$app->request->get('page');
        $perpage = Yii::$app->request->get('per-page');
        $params['pages'] = !empty($page)? intval($page) : 1;                    //第几页
        $params['pagessize'] = !empty($perpage) ? intval($perpage) :10 ;        //每页

        $OrderInfoDetail      = new OrderInfoDetail();
        $orderInfo            = $OrderInfoDetail->getOrderWap($params);

        $dataHtml             = $this->renderPartial('listwapajax',['orderInfo'=>$orderInfo['data'],'countdown'=>$params['pages']]);
        if($dataHtml ==''){
            return null;
        }

        return $dataHtml;
    }

    //查询订单详情
    public function actionDeatilwap(){

        //验证登录
        if(self::verification() == false)
            return  $this->redirect($this->login_url);

        //验证参数
        $order_info_id = intval(Yii::$app->request->get('order_info_id'));
        if(self::verification($order_info_id) == false)
            throw new NotFoundHttpException(true);

        //查询订单详情
        $OrderInfoDetail = new OrderInfoDetail();
        //查询条件
        $params['order_info.order_info_id'] = $order_info_id;
        $params['order_info.bestry_uid']    = $this->bestry_uid;
        $orderInfo = $OrderInfoDetail->getOrderInfo($params);

        //获取订单详情的信息
        if(empty($orderInfo))
            throw new NotFoundHttpException(true);

        //获取对应的商品信息
        $orderGoods     = $OrderInfoDetail->getOrderGoodsList($orderInfo['order_info_id'],$params['historyId']);
        //获取商品的总数量
        $orderGoodsNUM  = $OrderInfoDetail->getOrderGoodsNum($orderInfo['order_info_id'],$params['historyId']);


        return $this->render('deatilwap',['orderInfo'=>$orderInfo,'orderGoods'=>$orderGoods,'orderGoodsNUM'=>$orderGoodsNUM]);
    }

    //获取物流信息
    public function actionLogic(){
        //验证登录
        if(self::verification() == false)
            return  $this->redirect($this->login_url);

        //验证参数
        $order_info_id = intval(Yii::$app->request->get('order_info_id'));
        if(self::verification($order_info_id) == false)
            throw new NotFoundHttpException(true);

        //查询订单信息
        $OrderInfoDetail = new OrderInfoDetail();
        //查询条件
        $params['order_info.order_info_id'] = $order_info_id;
        $params['order_info.bestry_uid']    = $this->bestry_uid;
        $orderInfo =$OrderInfoDetail->getOrderInfos($params);

        if(empty($orderInfo))
            throw new NotFoundHttpException(true);

        //print_r($orderInfo);exit;
        //获取物流信息
        $orderInfo['delivery_code']         = $orderInfo['lgistics']['delivery_code'];

        $logicInfo = $OrderInfoDetail->getLogic($orderInfo['delivery_code'],$orderInfo['order_sn']);

        return $this->render('logic',['logicInfo'=>$logicInfo,'orderInfo'=>$orderInfo]);

    }

    //评论详情页面
    public function actionComreviews(){

        //验证登录
        if(self::verification() == false)
            return  $this->redirect($this->login_url);

        //验证参数
        $order_info_id = intval(Yii::$app->request->get('order_info_id'));
        if(empty($order_info_id) && !is_numeric($order_info_id)){
            throw new NotFoundHttpException(true);
        }

        //查询订单详情
        $OrderInfoDetail = new OrderInfoDetail();
        //查询条件
        $params['order_info.order_info_id'] = $order_info_id;
        $params['order_info.bestry_uid']    = $this->bestry_uid;
        $orderInfo = $OrderInfoDetail->getOrderInfo($params);

        if(empty($orderInfo))
            throw new NotFoundHttpException(true);

        //获取对应的商品信息
        $orderGoods = $OrderInfoDetail->getOrderGoodsList($orderInfo['order_info_id'],$params['historyId']);

        return $this->render('comreviews',['orderInfo'=>$orderInfo,'orderGoods'=>$orderGoods]);
    }

    //取消订单
    public function actionCancelOrder(){
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;   //依照json返回

        if(self::verification() == false)
            return ['flag' => false, 'msg' => '您未登陆，请登录', 'errCode' => ''];

        //验证参数
        $order_info_id = intval(Yii::$app->request->get('order_info_id'));
        if(self::verification($order_info_id) == false)
            return ['flag' => false, 'msg' => '您的操作有误！请重新操作', 'errCode' => ''];

        $order_info_id = Yii::$app->request->get('order_info_id');
        if(empty($order_info_id))
            return ['flag' => false, 'msg' => '订单参数错误', 'errCode' => ''];

        $orderInfo = OrderInfo::find()->where(['bestry_uid'=>$this->bestry_uid,'order_info_id'=>$order_info_id])->one();
        if(empty($orderInfo)){
            $orderInfo = OrderInfoHistory::find()->where(['bestry_uid'=>$this->bestry_uid,'order_info_id'=>$order_info_id])->one();
        }

        //取消订单
        if(!$orderInfo){
            return ['flag' => false, 'msg' => '暂无订单信息', 'errCode' =>''];
        }
        $order = new Order();
        $order->order_info_id   =$order_info_id;
        $order->bestry_uid      = $this->bestry_uid;

        if (!$order->cancelOrder()) {

            return ['flag' => false, 'msg' => '取消订单失败', 'errCode' => $order->getErrors()];
        }else
            return  ['flag' => true, 'msg' => '取消订单成功', 'errCode' => '200'];


    }

    //确认收货
    public function actionSureOrder(){
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;   //依照json返回

        if(self::verification() == false)
            return ['flag' => false, 'msg' => '您未登陆，请登录', 'errCode' => ''];

        //验证参数
        $order_info_id = intval(Yii::$app->request->get('order_info_id'));
        if(self::verification($order_info_id) == false)
            return ['flag' => false, 'msg' => '您的操作有误！请重新操作', 'errCode' => ''];

        if(!$order_info_id)
            return ['flag' => false, 'msg' => '订单参数错误！', 'errCode' =>'-400'];
        //3个月之前的订单
        $historyId      =0;
        $order_info     = OrderInfo::find()->select(['bestry_uid','status','pay_status','order_amount','order_info_id'])->where(['order_info_id'=>$order_info_id])->one();

        if(empty($order_info)){
            $historyId  = 1;
            $order_info = OrderInfoHistory::find()->select(['bestry_uid','status','pay_status','order_amount','order_info_id'])->where(['order_info_id'=>$order_info_id])->one();
        }

        if(!$order_info)
            return ['flag' => false, 'msg' => '暂无收货订单！', 'errCode' =>'-400'];

        if(0 == $historyId && Order::orderStatusChange($order_info->order_info_id,OrderInfoDetail::$STATUS_ORDER_OK,$order_info->pay_status))
            return  ['flag' => true,'data'=>$order_info, 'msg' => '确认收货成功！', 'errCode' => '200'];


        if(1 == $historyId && Order::orderStatusChangeHistory($order_info->order_info_id,OrderInfoDetail::$STATUS_ORDER_OK,$order_info->pay_status))
            return  ['flag' => true,'data'=>$order_info, 'msg' => '确认收货成功！', 'errCode' => '200'];
        
        return ['flag' => false, 'msg' => '确认收货失败！', 'errCode' =>'-400'];
    }

    private function verification($orderInfoId=''){
        //验证登录
         if(!$this->isLogin()){
           return false;
        }

        if(empty($this->bestry_uid) || $this->bestry_uid < 0){
            return false;
        }

        if(!empty($orderInfoId) && $orderInfoId < 0){
            return false;
        }

        return true;


    }

}